package com.hjj.java.se.objectRep;

import java.io.*;

public class SerializableObject implements Serializable {
    private static final long serialVersionUID = 1L;
    private String str0;
    private transient String str1;
    private static String str2 = "abc";

    public SerializableObject(String str0, String str1) {
        this.str0 = str0;
        this.str1 = str1;
    }

    public String getStr0() {
        return str0;
    }

    public String getStr1() {
        return str1;
    }

    public String getStr2() {
        return str2;
    }

    private void writeObject(ObjectOutputStream oos) throws Exception {
        System.out.println("自己控制序列化的过程");
        oos.defaultWriteObject();
        oos.writeInt(str1.length());
        for (int i = 0; i < str1.length(); i++) {
            oos.writeChar(str1.charAt(i));
        }
    }

    private void readObject(ObjectInputStream ois) throws Exception {
        System.out.println("自己控制反序列化");
        ois.defaultReadObject();
        int length = ois.readInt();
        char[] cs = new char[length];
        for (int i = 0; i < length; i++) {
            cs[i] = ois.readChar();
        }
        str1 = new String(cs, 0, length);
    }

    @Override
    public String toString() {
        return "SerializableObject{" +
                "str0='" + str0 + '\'' +
                ", str1='" + str1 + '\'' +
                ", str2='" + str2 + '\'' +
                '}';
    }
}
